﻿using AutoMapper;
using MediatR;
using Microsoft.Data.SqlClient;
using S.Authorization.API.Application.Command.OrganizationCommand;
using S.Authorization.Domain.Model.Organization;
using S.Authorization.Infrastructure;
using S.Authorization.Infrastructure.Interface.Organization;
using Yitter.IdGenerator;

namespace S.Authorization.API.Application.CommandHandlers.OrganizationCommandHandler
{
    public class BusinessModelCreateCommandHandle : IRequestHandler<BusinessModelCreateCommand, bool>
    {
        private readonly IBusinessModelRepository business;
        private readonly IMapper mapper;
        private readonly DbHelper dbHelper;
        public BusinessModelCreateCommandHandle(IBusinessModelRepository _business, IMapper _mapper,DbHelper _dbHelper)
        {
            this.business = _business;
            this.mapper = _mapper;
            this.dbHelper = _dbHelper;
        }
        /// <summary>
        /// 单位表添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> Handle(BusinessModelCreateCommand request, CancellationToken cancellationToken)
        {
            string sql = "SELECT * FROM [dbo].[BusinessModel] WHERE BusinessFullName =@BusinessFullName";
            var parameters = new List<SqlParameter>
            {
                  new SqlParameter("@BusinessFullName",request.BusinessFullName),
            };
            var result = await dbHelper.GetListDataBySql<SheepFarmsModel>(sql, parameters.ToArray());

            if (result.Count != 0)
            {
                string sqls = @"UPDATE BusinessModel
                                        SET 
                                            BusinessName = @BusinessName,
                                            BusinessLogo = @BusinessLogo,
                                            BusinessLabel = @BusinessLabel,
                                            ProvinceId = @ProvinceId,
                                            CityId = @CityId,
                                            AreaId = @AreaId,
                                            FullAddress = @FullAddress,
                                            BusinessPhone = @BusinessPhone,
                                            UnifiedSocialCreditCode = @UnifiedSocialCreditCode,
                                            BusinessDesc = @BusinessDesc,
                                            UpdateBy = @UpdateBy,
                                            UpdateTime = GETDATE()
                                        WHERE BusinessFullName = @BusinessFullName;";
                var parameter = new List<SqlParameter>
                {
                      new SqlParameter("@BusinessName",request.BusinessName),
                      new SqlParameter("@BusinessLogo",request.BusinessLogo),
                      new SqlParameter("@BusinessLabel",request.BusinessLabel),
                      new SqlParameter("@ProvinceId",request.ProvinceId),
                      new SqlParameter("@CityId",request.CityId),
                      new SqlParameter("@AreaId",request.AreaId),
                      new SqlParameter("@FullAddress",request.FullAddress),
                      new SqlParameter("@BusinessPhone",request.BusinessPhone),
                      new SqlParameter("@UnifiedSocialCreditCode",request.UnifiedSocialCreditCode),
                      new SqlParameter("@BusinessDesc",request.BusinessDesc),
                      new SqlParameter("@BusinessFullName",request.BusinessFullName),
                      new SqlParameter("@UpdateBy",request.CreateBy),
                };
                int res = await dbHelper.ExecuteNonQuery(sqls, parameter.ToArray());
                return res != 0 ? true : false; ;
            }
            else
            {
                BusinessModel columns = mapper.Map<BusinessModel>(request);
                return await business.AddAsync(columns);
            }
            
        }
    }
}
